home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / detex10.zip / DETEX.C next >
C/C++ Source or Header  |  1992-09-09  |  5KB  |  235 lines

  1. /* detex: to strip TeX's and LaTeX's commands
  2. * modified for Turbo C '91-'92: t.m.sanders@umich.edu
  3. * to compile:   tcc detex.c */
  4.  
  5. char *documentation[] = {
  6. " SYNTAX",
  7. "        detex -[i] file1 [file2 .....]",
  8. "     or detex -[i]  < file1 [file2 ....]",
  9. "",
  10. "See the manual page for more details.",
  11. "",
  12. "        -i flag:     ignores TeX's and LaTeX's \input and \include commands",
  13. "",
  14. };
  15.  
  16. /* Author: Kamal Al-Yahya, Stanford University,          11/1/83 */
  17. /* Modified: Kamal Al-Yahya                6/30/86 */
  18.  
  19. int    doclength = { sizeof documentation/sizeof documentation[0] };
  20.  
  21. #include        <stdio.h>
  22. #include <io.h> /* modified for Turbo-C header files -- tms */
  23.  
  24. int xargc;
  25. char **xargv;
  26.  
  27. main(argc,argv)
  28. int argc; 
  29. char *argv[];
  30. {
  31. FILE *temp;
  32. register char *cptr;
  33. int piped_in;
  34. int iflag,i;
  35.  
  36. /* If no arguments, and not in a pipeline, self document */
  37. piped_in = ioctl ((fileno (stdin)), 6); /* mofified for different ioctl-- tms */
  38. if (argc == 1 && !piped_in)
  39.     {
  40.     for( i=0; i<doclength; i++)
  41.         printf("%s\n",documentation[i]);
  42.     exit (0);
  43.     }
  44.  
  45. /* process option flags */
  46. xargc = argc;
  47. xargv = argv;
  48. for (xargc--,xargv++; xargc; xargc--,xargv++)
  49.     {
  50.     cptr = *xargv; 
  51.     if( *cptr=='-' )
  52.         {
  53.         while( *(++cptr))
  54.             {
  55.             switch( *cptr )
  56.                 {
  57.                 case 'i':
  58.                     iflag=1;
  59.                     break;
  60.                 default:
  61.                          fprintf(stderr,
  62.                     "unknown flag -%c\n",*cptr);
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69. /* first process pipe input */
  70. if(piped_in)
  71.     detex(stdin,&iflag);
  72.  
  73. /* then process input line for arguments and assume they are input files */
  74. xargc = argc;
  75. xargv = argv;
  76. for (xargc--,xargv++; xargc; xargc--,xargv++)
  77.     {
  78.     cptr = *xargv; 
  79.     if( *cptr=='-' ) continue;        /* this is a flag */
  80.     if((temp=fopen(cptr,"r")) != NULL)
  81.         detex(temp,&iflag);
  82.     else
  83.         fprintf(stderr,"detex: Cannot open %s\n",cptr);
  84.     }
  85.  
  86. }
  87.  
  88. #define MAXLEN 500
  89. detex(fp,iflag)            /* stripping TEX commands */
  90.  
  91. FILE *fp;
  92. int *iflag;
  93. {
  94. FILE *fpp;
  95. int c,c1;
  96. char w[MAXLEN];
  97. int i,j;
  98.  
  99. while ((c = getc(fp)) != EOF)
  100.     {
  101.     c1=' ' ;        /* initialize c1 */
  102.     switch (c)
  103.     {
  104. /* detect TeX commands (backslash) */
  105.     case '\\':
  106.         c1=c ;        /* c1 is needed to see if there is \$ */
  107.         c=' ' ;        /* "erase" the backslash */
  108.         putc(c,stdout);
  109.         while ((c = getc(fp)) != EOF)
  110.             {
  111.             /* see if input is the word */
  112.             if (c == 'i' && c1 == '\\')
  113.                 {
  114.                 c1=' ' ;        /* reinitialize c1 */
  115.                 i=0;
  116.                 w[i]=c;
  117.                 while ((c = getc(fp)) != ' ')
  118.                     {
  119.                     if(c=='\n' || c=='$' || c=='#' || c=='%'
  120.                     || c=='{' || c=='(' || c==')') break;
  121.                     w[++i]=c;
  122.                     }
  123.             if(w[1]=='n' && w[2]=='p' && w[3]=='u' && w[4]=='t'
  124.                || w[1]=='n' && w[2]=='c' && w[3]=='l' && w[4]=='u'
  125.                && w[5]=='d' && w[6]=='e')
  126.                     {
  127. /* if the word is input, get the file name */
  128.                     i=0;
  129.                     while (( c = getc(fp)) != ' ')
  130.                         {
  131.                         if (c == '\n' || c == '}') break;
  132.                         w[i++]=c;
  133.                         }
  134.                     for (j=i; j < MAXLEN; j++)
  135.                         w[j]='\0';
  136. /* if iflag is turned on, don't open files requested by \input) */
  137.                     if (*iflag != 1)
  138.                     {
  139.                     fpp=fopen(w, "r"); /* open the new file */
  140.                     if( fpp == NULL )
  141.                          {
  142. /* if file is not found, try file.tex  */
  143.                          w[i]='.';        w[i+1]='t';
  144.                          w[i+2]='e';    w[i+3]='x';
  145.                          fpp=fopen(w, "r");
  146.                          if( fpp == NULL )
  147.                         {
  148.                              fprintf(stderr,
  149.                         "detex: Cannot open %s\n",w);
  150.                             break;
  151.                         }
  152.                          }
  153.                     for (i=0; i < MAXLEN; i++)
  154.                         w[i]='\0';
  155.                     /* nested detex */
  156.                     detex(fpp,iflag);
  157.                     fclose(fpp);
  158.                     }
  159.                     }
  160.                 }
  161.             if (c == '$')            goto dollar;
  162.             if (c == '%')            goto comm;
  163.             if (c == '{' || c == '#')    break;
  164.             if (c == ' ' || c == '\n' || c == '(' || c == ')')
  165.                 {
  166.                 putc(c,stdout);
  167.                 break;
  168.                 }
  169.             c1=' ' ;            /* reinitialize c1 */
  170.             }
  171.         break;
  172. /* come here if the character is dollar sign */
  173. dollar:
  174.     case '$':
  175.         c=' ' ;        /* "erase" the dollar sign */
  176.         putc (c,stdout);
  177.         if (c1 == '\\')         break;        /* means \$ */
  178.         c = getc(fp);
  179.         if(c == '$')        goto dollar2;
  180.         else            goto dollar1;
  181.         break;
  182.  
  183. /* see if it is an in-line equation (delimeter is one dollar sign) */
  184. dollar1:
  185.         while((c = getc(fp)) != EOF)
  186.             if(c == '$')    break;
  187.         break;
  188.  
  189. /* see if it is a displayed equation (delimeter is two dollar signs) */
  190. dollar2:
  191.         while((c = getc(fp)) != EOF)
  192.             {
  193.             if(c == '$')
  194.                 {
  195.                 c = getc(fp);
  196.                 if (c != '$') fprintf(stderr,
  197.             "detex: unmatched dollar signs, I will ignore that\n");
  198.                 break;
  199.                 }
  200.             }
  201.         break;
  202.  
  203. /* ignore commented text */
  204. comm:
  205.     case '%':
  206.         if (c1 == '\\')         break;        /* means \% */
  207.         while((c = getc(fp)) != EOF)
  208.             if (c == '\n')
  209.                 {
  210.                 putc(c,stdout);
  211.                 break;
  212.                 }
  213.         break;
  214. /* erase these character */
  215.     case '{':
  216.         c=' ';
  217.     case '}':
  218.         c=' ';
  219.     case '_':
  220.         c=' ';
  221.     case '^':
  222.         c=' ';
  223.     case '&':
  224.         c=' ';
  225.     case '#':
  226.         c=' ';
  227. /* default is doing nothing: pass the character to the output */
  228.     default:
  229.         putc(c,stdout);
  230.         break;
  231.     }
  232.     }
  233. }
  234.  
  235.